import { Vector2, Camera, Object3D, Raycaster,Intersection } from "three";
import { windowMouseTo3DMouse } from "@/utils//Mouse"
import { IDomWH } from "@/interface/common";

export class RaycasterControls {
    camera: Camera;
    mouse: Vector2 = new Vector2();
    domParams: IDomWH
    constructor(camera: Camera, domParams: IDomWH) {
        this.camera = camera;
        this.domParams = domParams;
    }

    setDomParams(domParams: IDomWH){
        this.domParams = domParams;
    }

    /**
     * 鼠标穿过的模型
     * @param meshList 是要被检测是否穿过的物体
     * @param filter 过滤掉不需要检测的部分
     * @returns {
    *  raycasterMesh: 被穿过的物体集合
    *  firstMesh：第一个被穿过物体的mesh
    *  firstData：第一个被穿过物体的总数据
    *  isRayToMesh: 是否有物体被射线穿过
    * }
    */
    getMouseRayToMesh(meshList: Object3D[],filter?:(item: Intersection<Object3D<THREE.Event>>[]) => Intersection<Object3D<THREE.Event>>[]) {
        let raycaster = new Raycaster();
        let _mouse = windowMouseTo3DMouse(this.mouse, this.domParams);
        raycaster.setFromCamera(_mouse, this.camera);
        let raycasterMesh = raycaster.intersectObjects(meshList); // 穿过的物体
        if(filter){
            raycasterMesh = filter(raycasterMesh);
        }
        return {
            raycasterMesh,
            firstMesh: raycasterMesh[0]?.object,
            firstData: raycasterMesh[0],
            isRayToMesh: raycasterMesh.length !== 0
        };
    }

}



